<?php

session_start();


require_once '../common/config.php';
require_once '../common/conn.php';
require_once '../common/function.php';
require_once '../db/account.db.php';

$action = $_POST['action'];

switch ($action) {
    case 'addCart':
        $res = addCart();
        break;
    case 'deleteCart':
        $res = deleteCart();
        break;
    case 'confirmBuy':
        $res = confirmBuy();
        break;
    default:
        $res['code'] = 2;
        $res['msg'] = '申请类型错误';
}

echo json_encode($res);

/**
 * 添加到购物车
 */
function addCart() {
    $itemcode = isset($_POST['item_code']) ? addslashes(trim($_POST['item_code'])) : "";
    $array = array();
    if (isset($_SESSION['cart'])) {
        $array = $_SESSION['cart'];
    }
    array_push($array, $itemcode);

    $_SESSION['cart'] = $array;
    $res['code'] = 0;
    return $res;
}

/**
 * 删除购物车中的商品
 * @return int
 */
function deleteCart() {
    $itemcode = isset($_POST['item_code']) ? addslashes(trim($_POST['item_code'])) : "";

    $array = array();
    $newArray = array();
    if (isset($_SESSION['cart'])) {
        $array = $_SESSION['cart'];
        $newArray = $_SESSION['cart'];
    }
    for ($i = 0; $i < count($array); $i++) {
        if ($array[$i] == $itemcode) {
            unset($newArray[$i]);
        }
    }
    $array = array();
    $key = 0;
    foreach ($newArray as $value) {
        $array[$key] = $value;
        $key++;
    }
    $_SESSION['cart'] = $array;
    unset($newArray);

    $res['code'] = 0;
    return $res;
}

/**
 * 购买道具
 */
function confirmBuy() {
    $price = isset($_POST['price']) ? floatval(trim($_POST['price'])) : "";
    $itemNo = isset($_POST['itemNo']) ? addslashes(trim($_POST['itemNo'])) : "";
    $id_x = isset($_POST['id_x']) ? addslashes(trim($_POST['id_x'])) : "";
    $buy = isset($_POST['buy']) ? addslashes(trim($_POST['buy'])) : "";
    $haoyou = isset($_POST['haoyou']) ? addslashes(trim($_POST['haoyou'])) : "";

//    file_put_contents("post.txt", json_encode($_POST));

    $dbc = new DBC();
    $sql = "SELECT * FROM AccountMoney WHERE AccUnique = " . $_SESSION['AccUnique'];
    $data = $dbc->fetchData($sql);

    if (isArrayNotEmpty($data) && $data[0]['Balance'] < $price) {
        $res['code'] = 2;
        $res['msg'] = "余额不足";
        return $res;
    } else {
        $sqls = array();
        $sql = "UPDATE AccountMoney SET Balance = Balance - " . $price . " WHERE AccUnique = " . $_SESSION['AccUnique'];
        array_push($sqls, $sql);

        $unionNo = date("YmdHis") . "_" . $_SESSION['AccUnique'];
        $num = 1;
        $sql = "INSERT INTO AccountMoneyDetail(AccUnique,ItemIdx,ItemCount,UpdateTime,ItemNo,UnionNo) "
                . "VALUES('" . $_SESSION['AccUnique'] . "','" . $id_x . "','" . $num . "',getDate(),'" . $itemNo . "','" . $unionNo . "')";
        array_push($sqls, $sql);

        if ($buy=="true") {

            $sql = "INSERT INTO item_buy_info(accunique,account_id,product_id,ItemIndex,Item_count,Item_using,buy_dated) "
                    . "VALUES('" . $_SESSION['AccUnique'] . "','" . $_SESSION['AccountID'] . "','139','"
                    . $itemNo . "','" . $num . "',3,getDate())";
            array_push($sqls, $sql);
        } else {
            $sql = "SELECT * FROM Account WHERE AccUnique = '" . $haoyou . "'";
            $data10 = $dbc->fetchData($sql);
            if (count($data10) > 0) {
                $sql = "INSERT INTO item_buy_info(accunique,account_id,product_id,ItemIndex,Item_count,Item_using,buy_dated) "
                        . "VALUES('" . $haoyou . "','" . $data10[0]['AccountID'] . "','139','"
                        . $itemNo . "','" . $num . "',3,getDate())";
                array_push($sqls, $sql);
            }
        }


        $result = $dbc->executeBatch($sqls);

        $dbc->close();

        if ($result) {
            $balance = $data[0]['Balance'] - $price;
            $res['code'] = 0;
            $res['msg'] = "";
            $res['balance'] = $balance;
        } else {
            $res['code'] = 1;
            $res['msg'] = "失败";
        }
        return $res;
    }
}
